package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.CommunityNewsBean;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/2/1.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class CommunityNewsAdapter extends RecyclerArrayAdapter<CommunityNewsBean.DataBean.ListBean> {

    private CommunityNewInterface communityNewInterface;

    public void setCommunityNewInterfaceListener(CommunityNewInterface communityNewInterface) {
        this.communityNewInterface = communityNewInterface;
    }

    public CommunityNewsAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new CommunityNewsHolder(parent);
    }


    class CommunityNewsHolder extends BaseViewHolder<CommunityNewsBean.DataBean.ListBean> {


        @Bind(R.id.img_head)
        ImageView imgHead;
        @Bind(R.id.tv_user_name)
        TextView tvUserName;
        @Bind(R.id.img_type)
        TextView imgType;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.delete)
        TextView delete;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.tv_theme)
        TextView tvTheme;

        public CommunityNewsHolder(ViewGroup parent) {
            super(parent, R.layout.item_community_news);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void setData(final CommunityNewsBean.DataBean.ListBean data) {
            super.setData(data);
            Glide.with(BaseActivity.context)
                    .load(data.portrait)
                    .apply(MyApplication.headOptions)
                    .into(imgHead);
            tvUserName.setText(data.nickname);
            tvTime.setText(data.addtime);
            tvContent.setText(data.content);

            switch (data.type){
                case 1:
                    imgType.setText("评论我的");
                    tvTheme.setText("主题："+data.activity.title);
                    delete.setVisibility(View.GONE);
                    break;
                case 2:
                    imgType.setText("我的评论");
                    tvTheme.setText("主题："+data.activity.title);
                    delete.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    imgType.setText("我的回复");
                    tvTheme.setText("主题："+data.comment.content);
                    break;
                case 4:
                    imgType.setText("回复我的");
                    tvTheme.setText("主题："+data.comment.content);
                    break;

            }

            if (data.own==2){
                delete.setVisibility(View.VISIBLE);
            }else {
                delete.setVisibility(View.GONE);
            }

            delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (communityNewInterface!=null){
                        communityNewInterface.delete(getDataPosition());
                    }
                }
            });
            tvTheme.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   if (communityNewInterface!=null){

                       switch (data.type){
                           case 1:
                           case 2:
                               communityNewInterface.goFindDetail(getDataPosition());
                               break;
                           case 3:
                           case 4:
                               communityNewInterface.goCommentDetail(getDataPosition());
                               break;

                       }
                   }
                }
            });

        }
    }


    public interface CommunityNewInterface {
        void delete(int position);
        void goFindDetail(int position);
        void goCommentDetail(int position);
    }

}
